package cn.iocoder.yudao.module.yyzx.service.rooms;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.yyzx.controller.admin.rooms.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.rooms.RoomsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.yyzx.dal.mysql.rooms.RoomsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.yyzx.enums.ErrorCodeConstants.*;

/**
 * 房间信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RoomsServiceImpl implements RoomsService {

    @Resource
    private RoomsMapper roomsMapper;

    @Override
    public Integer createRooms(RoomsSaveReqVO createReqVO) {
        // 插入
        RoomsDO rooms = BeanUtils.toBean(createReqVO, RoomsDO.class);
        roomsMapper.insert(rooms);
        // 返回
        return rooms.getId();
    }

    @Override
    public void updateRooms(RoomsSaveReqVO updateReqVO) {
        // 校验存在
        validateRoomsExists(updateReqVO.getId());
        // 更新
        RoomsDO updateObj = BeanUtils.toBean(updateReqVO, RoomsDO.class);
        roomsMapper.updateById(updateObj);
    }

    @Override
    public void deleteRooms(Integer id) {
        // 校验存在
        validateRoomsExists(id);
        // 删除
        roomsMapper.deleteById(id);
    }

    private void validateRoomsExists(Integer id) {
        if (roomsMapper.selectById(id) == null) {
            throw exception(ROOMS_NOT_EXISTS);
        }
    }

    @Override
    public RoomsDO getRooms(Integer id) {
        return roomsMapper.selectById(id);
    }

    @Override
    public PageResult<RoomsDO> getRoomsPage(RoomsPageReqVO pageReqVO) {
        return roomsMapper.selectPage(pageReqVO);
    }

}